import java.sql.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.io.*;
import java.io.FileOutputStream;
public class Backup
{
	public static void backupAll() throws SQLException
	{
		backupUser();
		backupFacility();
		backupVenue();
		backupBooking();
		backupReserve();
	}
	
	public static void backupUser() throws SQLException
	{
		try
		{
			FileOutputStream out = new FileOutputStream("user.txt");
			PrintStream p = new PrintStream(out);

			Connector conn = new Connector("select * from user");
			ResultSet rs = conn.execute();
			p.println("userid;firstname;lastname;username;password;license;status;role;expired");
			while(rs.next())
			{
				String userid = rs.getString("userid");
				String firstname = rs.getString("firstname");
				String lastname = rs.getString("lastname");
				String username = rs.getString("username");
				String password = rs.getString("password");
				String license = rs.getString("license");
				String status = rs.getString("status");
				String role = rs.getString("role");
				String expired = rs.getString("expired");
				p.println(userid + ";" + firstname + ";" + lastname + ";" + username + ";" + password + ";" + license + ";" + status + ";" + role + ";" + expired);
			}
			rs.close();
		}
		catch (FileNotFoundException e)
		{
		
		}
	}

	public static void backupFacility() throws SQLException
	{
		try
		{
			FileOutputStream out = new FileOutputStream("facility.txt");
			PrintStream p = new PrintStream(out);

			Connector conn = new Connector("select * from facility");
			ResultSet rs = conn.execute();
			p.println("facilityid;facilityname;status");
			while(rs.next())
			{
				String facilityid = rs.getString("facilityid");
				String facilityname = rs.getString("facilityname");
				String status = rs.getString("status");
				p.println(facilityid + ";" + facilityname + ";" + status);
			}
			rs.close();
		}
		catch (FileNotFoundException e)
		{
		
		}
	}

	public static void backupVenue() throws SQLException
	{
		try
		{
			FileOutputStream out = new FileOutputStream("venue.txt");
			PrintStream p = new PrintStream(out);

			Connector conn = new Connector("select * from venue");
			ResultSet rs = conn.execute();
			p.println("venueid;venuename;facilityid;capacity;requirelic;status");
			while(rs.next())
			{
				String venueid = rs.getString("venueid");
				String venuename = rs.getString("venuename");
				String facilityid = rs.getString("facilityid");
				String capacity = rs.getString("capacity");
				String requirelic = rs.getString("requirelic");
				String status = rs.getString("status");
				p.println(venueid + ";" + venuename + ";" + facilityid + ";" + capacity + ";" + requirelic + ";" + status);
			}
			rs.close();
		}
		catch (FileNotFoundException e)
		{
		
		}
	}

	public static void backupBooking() throws SQLException
	{
		try
		{
			FileOutputStream out = new FileOutputStream("booking.txt");
			PrintStream p = new PrintStream(out);

			Connector conn = new Connector("select * from booking");
			ResultSet rs = conn.execute();
			p.println("bookingid;userid;facilityid;venueid;bookingstart;bookingend");
			while(rs.next())
			{
				String bookingid = rs.getString("bookingid");
				String userid = rs.getString("userid");
				String facilityid = rs.getString("facilityid");
				String venueid = rs.getString("venueid");
				String bookingstart = rs.getString("bookingstart");
				String bookingend = rs.getString("bookingend");
				p.println(bookingid + ";" + userid + ";" + facilityid + ";" + venueid + ";" + bookingstart + ";" + bookingend);
			}
			rs.close();
		}
		catch (FileNotFoundException e)
		{
		
		}
	}

	public static void backupReserve() throws SQLException
	{
		try
		{
			FileOutputStream out = new FileOutputStream("reserve.txt");
			PrintStream p = new PrintStream(out);

			Connector conn = new Connector("select * from reserve");
			ResultSet rs = conn.execute();
			p.println("reserveid;facilityid;reservedate");
			while(rs.next())
			{
				String reserveid = rs.getString("reserveid");
				String facilityid = rs.getString("facilityid");
				String reservedate = rs.getString("reservedate");
				p.println(reserveid + ";" + facilityid + ";" + reservedate);
			}
			rs.close();
		}
		catch (FileNotFoundException e)
		{
		
		}
	}
}
